<?php
namespace app\controllers;
use app\models\ApplyModel;
use app\models\CourseModel;
use wlcat\src\plugin\Page;

//课程
class ApplyController extends AdminController {
    public function index($id = null)
    {
        $applyModel = new ApplyModel();
        $count      = $applyModel->get('select count(*) as total from '.$applyModel->getTable());
        $pagesize  = 15;
        $nowPage    = $this->request->input('get.p');
        $nowPage    = empty($nowPage) ? 1 : $nowPage;
        $startLimit = ($nowPage-1)*$pagesize;
        $list       = $applyModel->getAll('select * from '.$applyModel->getTable()." order by is_read asc, id desc limit {$startLimit}, $pagesize ");
        $course     = (new CourseModel())->selectAll();
        if (!empty($course))
        {
            $course = array_column($course, 'name', 'id');
        }
        $this->assign('course', $course);
        $page = new Page($count['total'], $pagesize, array(
            'c' => 'apply',
            'a' => 'index',
            'p' => $this->request->input('get.p')
        ));
        $page->setConfig('first','首页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->lastSuffix=false;//要想改变最后一页的样式，是要设置为false的，不然不会成功，上面的源码有解读为什么会这样
        $page->setConfig('last','尾页');
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }
    public function view($id = null)
    {
        $item = (new ApplyModel)->select($id);
        $course = (new CourseModel())->selectAll();
        if (!empty($course))
        {
            $course = array_column($course, 'name', 'id');
        }
        $this->assign('course', $course);
        $this->assign('info', $item);
        $this->display();
    }

    public function add()
    {
        $this->display();
    }

    public function update()
    {
        $id = $this->request->input('post.id');
        if (!intval($id) > 0)
        {
            echo 'id错误';
            die;
        }

        $name           = $this->request->input('post.name');
        $phone          = $this->request->input('post.phone');
        $remark         = $this->request->input('post.remark');
        $is_read        = $this->request->input('post.is_read');
        $course_id      = $this->request->input('post.course_id');
        (new ApplyModel())->update($id, array(
            'name'          => $name,
            'phone'         => $phone,
            'remark'         => $remark,
            'is_read'       => $is_read,
            'course_id'     => $course_id
        ));
        $this->success('更新成功', admin_url('apply/index'), 1);
    }

    public function destroy($id)
    {
        (new ApplyModel())->delete($id);
        $this->success('删除成功', admin_url('apply/index'), 1);
    }
    public function read($id)
    {
        (new ApplyModel())->update($id, array(
            'is_read'   => 1
        ));
        $this->success('设为已读成功', admin_url('apply/index'), 1);
    }
}